/*
 *@description: 家辅首页模块
 *@author: wudandan
 *@date: 2021-01-21 13:36:46
*/
'use strict';

import { homeProService } from '@/api';
const mutationTypes = {
    SET_SWITCHGRADE: 'setSwitchGrade',
    SET_SUBJECT: 'setSubject',
    SET_CAL: 'setCal',
    SET_TAB: 'setTabHeight',
    SET_REFRESH: 'setRefresh',
    SET_SWIPEABLE: 'setSwipeable',
    SET_SWICHVERSION: 'setSwitchVersion'
  },
  actionTypes = {
    SET_ITEM: 'setNextItemAction',
    SET_BOOK: 'setBookAction',
    SET_CATALOG: 'setCatalogAction',
    SET_CATALOGBYBOOK: 'setCatalogByBookAction',
    SET_SUBJECT: 'setSubject',
    SET_CAL: 'setCal',
    SET_REFRESH: 'setRefresh'
  },
  gettersTypes = {
    GET_SUBJECT: 'getSubject',
    GET_CAL: 'getCal',
    GET_TAB: 'getTabHeight',
    GET_REFRESH: 'getRefresh',
    GET_SWIPEABLE: 'getSwipeable',
    GET_SWItCHVERSION: 'getSwitchVersion',
    GET_SWItCHGRADE: 'getSwitchGrade'
  },
  state = {
    tabHeight: 20,
    bookInfo: {},
    swipeable: true,
    pullRefresh: true,
    isSwitchVersion: false,
    switchGrade: false,
    subject: {}, // 缓存科目信息
    currentCalInfo: {} // 当前 选中年级学段学段学期
  },
  mutations = {
    [mutationTypes.SET_SUBJECT] (state, msg = {}) {
      state.subject = msg;
    },
    [mutationTypes.SET_CAL] (state, msg = {}) {
      state.currentCalInfo = msg;
    },
    [mutationTypes.SET_TAB] (state, tabHeight) {
      state.tabHeight = tabHeight;
    },
    [mutationTypes.SET_SWICHVERSION] (state, isSwitchVersion) {
      state.isSwitchVersion = isSwitchVersion;
    },
    [mutationTypes.SET_REFRESH] (state, refresh) {
      state.pullRefresh = refresh;
    },
    [mutationTypes.SET_SWITCHGRADE] (state, switchGrade) {
      state.switchGrade = switchGrade;
    },
    [mutationTypes.SET_SWIPEABLE] (state, swipeable) {
      state.swipeable = swipeable;
    }
  },
  actions = {
    // 根据模板编码以及模板树节点的前几级节点编码，获取子节点元素列表
    async [actionTypes.SET_ITEM] ({ commit, state }, params) {
      const res = await homeProService.getTemplateNextItems(params);
      let nextItems = [];
      if (res.code === '200') {
        nextItems = res.data;
      }
      return nextItems;
    },
    // 获取辅学的特定学科可选择的教材和默认的章节
    async [actionTypes.SET_BOOK] ({ commit, state }, params) {
      const res = await homeProService.getTagBookCat(params);
      let bookInfo = {};
      if (res.code === '200') {
        bookInfo = res.data;
        return bookInfo;
      } else {
        return res;
      }
    },
    // 上报教材的目录进度
    async [actionTypes.SET_CATALOG] ({ commit, state }, params) {
      const res = await homeProService.addBookRecord(params);
      return res;
    },
    // 获取教材的目录进度
    async [actionTypes.SET_CATALOGBYBOOK] ({ commit, state }, params) {
      const res = await homeProService.getBookCatalog(params);
      let bookCatalog = {};
      if (res.code === '200') {
        bookCatalog = res.data;
      }
      return bookCatalog;
    },
    [actionTypes.SET_SUBJECT] ({ commit }, msg) {
      commit(mutationTypes.SET_SUBJECT, msg);
    },
    [actionTypes.SET_CAL] ({ commit }, msg) {
      commit(mutationTypes.SET_CAL, msg);
    },
    [actionTypes.SET_REFRESH] ({ commit }, msg) {
      commit(mutationTypes.SET_REFRESH, msg);
    }
  },
  getters = {
    [gettersTypes.GET_SUBJECT] (state) {
      return state.subject;
    },
    [gettersTypes.GET_CAL] (state) {
      return state.currentCalInfo;
    },
    [gettersTypes.GET_TAB] (state) {
      return state.tabHeight;
    },
    [gettersTypes.GET_SWItCHVERSION] (state) {
      return state.isSwitchVersion;
    },
    [gettersTypes.GET_REFRESH] (state) {
      return state.pullRefresh;
    },
    [gettersTypes.GET_SWItCHGRADE] (state) {
      return state.switchGrade;
    },
    [gettersTypes.GET_SWIPEABLE] (state) {
      return state.swipeable;
    }
  };
export default {
  namespaced: true,
  state,
  mutationTypes,
  actionTypes,
  gettersTypes,
  mutations,
  actions,
  getters
};
